﻿using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace BLL
{
    public abstract class BaseBLL<T>:IBaseBLL<T> where T:class,new()
    {
        #region 架构信息
        public BaseBLL()
        {
            SetDAL();
        }
        public IBaseDAL<T> idal;

        public abstract void SetDAL();

        private IDBSession iDbSession;
        
        public IDBSession DBSession
        {
            get
            {
                if (iDbSession == null)
                {
                    IDBSessionFactory DBSFactory = DI.Spring.GetObject<IDBSessionFactory>("DBSessFactory");
                    iDbSession = DBSFactory.GetDBSession();
                }
                return iDbSession;
            }
        }

        
        #endregion
     
        public virtual int ADD(T model)
        {
            return idal.ADD(model);
        }

        public virtual int Del(T model)
        {
            return idal.Del(model);
        }

        public virtual int DelByWhere(Expression<Func<T, bool>> delwhere)
        {
            return idal.DelByWhere(delwhere);
        }

        public virtual int Modify(T model, params string[] ProNames)
        {
            return idal.Modify(model, ProNames);
        }

        public virtual int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] ModifiedProNames)
        {
            return idal.Modify(model, whereLambda, ModifiedProNames);
        }

        public virtual List<T> GetListByWhere(Expression<Func<T, bool>> whereLambda)
        {
            return idal.GetListByWhere(whereLambda);
        }

        public virtual List<T> GetListByWhere<K>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, K>> orderLambda)
        {
            return idal.GetListByWhere(whereLambda, orderLambda);
        }

        public virtual List<T> GetPageList<K>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, K>> orderLambda)
        {
            return idal.GetPageList(pageIndex, pageSize, whereLambda, orderLambda);
        }

        public virtual List<T> GetPagedList<K>(int pageIndex, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, K>> orderBy, bool isAsc = true)
        {
            return idal.GetPagedList(pageIndex, pageSize, ref rowCount, whereLambda, orderBy, true);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public int ExcuteSql(string strSql, params object[] paras)
        {
           return  idal.ExcuteSql(strSql, paras);
        }

        /// <summary>
        /// 查询sql语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strSql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public List<T> SqlQuery<T>(string strSql, params object[] paras)
        {
            return idal.SqlQuery<T>(strSql, paras);
        }
    }
}
